﻿using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using StockUtil;
using System.Drawing;

namespace StockUI
{
    [DesignTimeVisible(false)]
    public class FormAbstract : Form
    {
        private const int WM_USER = 0x0400;
        private const int WM_HOTKEY = 0x312;
        private const int WM_FORMNOTIFY = WM_USER + 100;

        public FormAbstract()
        {
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }

        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_FORMNOTIFY:
                    GCHandle gch = GCHandle.FromIntPtr(m.LParam);
                    FormNotifyMessage msg = (FormNotifyMessage)gch.Target;
                    ProcessNotifyMessage(msg);
                    break;

                case WM_HOTKEY:
                    HotKeyHelper.ProcessHotKey(m);
                    break;

                default:
                    base.DefWndProc(ref m);
                    break;
            }
        }

        protected void SendMessage(FormAbstract form, FormNotifyMessage msg)
        {
            msg.Sender = this;
            msg.Receiver = form;

            GCHandle gch = GCHandle.Alloc(msg);
            _SendMessage(form.Handle, WM_FORMNOTIFY, IntPtr.Zero, GCHandle.ToIntPtr(gch));
            gch.Free();
        }

        protected virtual void ProcessNotifyMessage(FormNotifyMessage msg)
        {
        }

        protected void RegisterHotKey(HotKeyModifiers modifiers, Keys key, HotKeyHanlder handler)
        {
            HotKeyHelper.Register(this.Handle, modifiers, key, handler);
        }

        protected void UnregisterHotKey(HotKeyHanlder handler)
        {
            HotKeyHelper.Unregister(this.Handle, handler);
        }

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int _SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
    }

    public class FormNotifyMessage
    {
        public string Message { get; set; }
        public object Param1 { get; set; }
        public object Param2 { get; set; }
        public object Param3 { get; set; }
        public object Param4 { get; set; }
        public object Param5 { get; set; }
        public FormAbstract Sender { get; set; }
        public FormAbstract Receiver { get; set; }

        public FormNotifyMessage(string message, params object[] parameters)
        {
            Message = message;
            if (parameters.Length >= 1)
                Param1 = parameters[0];
            if (parameters.Length >= 2)
                Param2 = parameters[1];
            if (parameters.Length >= 3)
                Param3 = parameters[2];
            if (parameters.Length >= 4)
                Param4 = parameters[3];
            if (parameters.Length >= 5)
                Param5 = parameters[4];
        }
    }
}
